package RKUjava.lang;

/** A number of mathematical functions and constants. Some of the constants are 
    defined from Sun's java.lang.Math. This package defines a number of extra constants
    often used in calculations.
*/
public class RKUMath 
{

  public static final double E= Math.E;
  public static final double PI = Math.PI;
  public static final double HALFPI = Math.PI/2;
  public static final double ONETHIRDPI = Math.PI/3;
  public static final double QUARTERPI = Math.PI/4;
  public static final double TWOPI = 2*Math.PI;
  public static final double SQRT2 = Math.sqrt(2);
  public static final double SQRT3 = Math.sqrt(3);

  /** Normalize a value with respect to the interval between upper and lower. 
      The normalization is done by a linear scaling and transformation by the
      following function.<br>
      normvalue = (value-lower)/(upper-lower)
      @param value The value to normalize.
      @param lower The lower end of the interval.
      @param upper The upper end of the interval.

  */
  public static final double normalizeUniform(double value, double lower, double upper)
    {
      return (value-lower)/(upper-lower);

    }

  /** Normalize a value with respect to the interval between upper and lower. 
      The normalization is done by a linear scaling and transformation by the
      following function.<br>
      normvalue = (value-lower)/(upper-lower)
      @param value The value to normalize.
      @param lower The lower end of the interval.
      @param upper The upper end of the interval.
  */
  public static final double normalizeUniform(int value, int lower, int upper)
    {
      return ((double)(value-lower))/(upper-lower);

    }

  /** Normalize a value using the arctan function. The normalization is done
      by the following function:<br>
      normvalue = PI/2 + arctan(value)/PI;
      @param value The value to normalize.
  */
  public static final double normalizeArcTan(double value)
    {
      return (Math.PI/2) + Math.atan(value)/Math.PI;
    }


  /** Determine whether the value is in between upper and lower (lower&lt;=value&lt;=upper). 
      If the value passed in "lower" is greather than
      the value in "upper" the method uses upper&lt;=value&lt;=lower instead.
      @param value The value to test for. 
      @param lower The lower end of the interval.
      @param upper The upper end of the interval.
  */
  public static final boolean inClosedRange(double value, double lower, double upper)
    {
      if (lower>upper)
	return (upper<=value && value<=lower);
      else
	return (lower<=value && value<=upper);
    }

  /** Bring a value in between upper and lower (lower&lt;=value&lt;=upper). 
      This method will bring <tt>value</tt> into the range between 
      <tt>upper</tt> and <tt>lower</tt>. 
      @param value The value to test for. 
      @param lower The lower end of the interval.
      @param upper The upper end of the interval.
      @return If the <tt>value</tt> is greater than <tt>upper</tt> then 
      the returned value is <tt>upper</tt>, if <tt>value</tt> is less 
      than <tt>lower</tt> then <tt>lower</tt> is returned. Otherwise 
      <tt>value</tt> is returned.
  */
  public static final double bringInClosedRange(double value, double lower, double upper)
    {
	return Math.max(lower, Math.min(upper, value));
    }

  /** Log of argument with base.
      @param value The value to calculate log of.
      @param base The base of the logarithmic function.
      @return log<sub>base</sub>(value)
  */
  public static final double log(double value, double base)
    {
      return Math.log(value)/Math.log(base);
    }

  /** Get sign of a double value.
      @param value The value to get the sign from.
      @return -1 if value<0, 0 if value=0 and 1 if value>0
  */
  public static final int sgn(double value)
    {
      if (value<0)
	return -1;
      else if (value>0)
	return 1;
      else 
	return 0;
    }

  /** Get sign of a int value.
      @param value The value to get the sign from.
      @return -1 if value<0, 0 if value=0 and 1 if value>0
  */
  public static final int sgn(int value)
    {
      if (value<0)
	return -1;
      else if (value>0)
	return 1;
      else 
	return 0;
    }

  /** Square a value. 
   @param value The value to be squared.
   @return <tt>value*value</tt>
  */
  public static final double square(double value)
    {
      return value*value;
    }


}

